package com.bw.services.device.viewmodel

import androidx.lifecycle.Lifecycle
import androidx.lifecycle.repeatOnLifecycle
import androidx.lifecycle.viewModelScope
import com.bw.common.utils.LogUtil
import com.bw.lib_base.base.BaseViewModel
import com.bw.services.device.model.AddDeviceBody
import com.bw.services.device.model.DeviceEntity
import com.bw.services.device.model.DeviceRepo
import com.bw.services.entity.CarManage
import com.bw.services.model.ServicesRepo
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class DeviceViewModel@Inject constructor(val repo: DeviceRepo): BaseViewModel() {
    private val _devicesData = MutableStateFlow<DeviceEntity?>(null)
    val devicesData: MutableStateFlow<DeviceEntity?> =_devicesData
    fun getDevice(){
        viewModelScope.launch {
            LogUtil.d("===","getDevice:执行了")
            if(isInitLife()){
                LogUtil.d("===","生命周期:执行了")
                lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED){
                    val result = repo.getDevice()
                    _devicesData.emit(result)
                }
            }
        }
    }
    private val _AddDevicesData = MutableStateFlow<AddDeviceBody?>(null)
    val addDevicesData: MutableStateFlow<AddDeviceBody?> =_AddDevicesData
    fun AddDevice(data:AddDeviceBody){
        viewModelScope.launch {
            LogUtil.d("===","addDevice:执行了")
            if(isInitLife()){
                LogUtil.d("===","生命周期:执行了")
                lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED){
                    val result = repo.AddDevice(data)
                    _AddDevicesData.emit(result)
                }
            }
        }
    }
}